var WxParse = require("../../wxParse/wxParse.js"), shoppingCart = require("../../components/shopping_cart/shopping_cart.js"), specificationsModel = require("../../components/specifications_model/specifications_model.js"), gSpecificationsModel = require("../../components/goods/specifications_model.js"), goodsBanner = require("../../components/goods/goods_banner.js"), goodsInfo = require("../../components/goods/goods_info.js"), goodsBuy = require("../../components/goods/goods_buy.js"), goodsRecommend = require("../../components/goods/goods_recommend.js"), p = 1, is_loading_comment = !1, is_more_comment = !0, share_count = 0;

Page({
  data: {
    kc: '',
    is_new_user: true,
    tc: true,
    pageType: "STORE",
    id: null,
    goods: {},
    msgList: [],
    show_attr_picker: !1,
    form: {
      number: 1
    },
    tab_detail: "active",
    tab_comment: "",
    comment_list: [],
    comment_count: {
      score_all: 0,
      score_3: 0,
      score_2: 0,
      score_1: 0
    },
    autoplay: !1,
    hide: "hide",
    show: !1,
    x: getApp().core.getSystemInfoSync().windowWidth,
    y: getApp().core.getSystemInfoSync().windowHeight - 20,
    page: 1,
    drop: !1,
    goodsModel: !1,
    goods_num: 0,
    temporaryGood: {
      price: 0,
      num: 0,
      use_attr: 1
    },
    goodNumCount: 0,
    //点赞图片
    before: "/images/before.jpg",
    after: "/images/after.jpg",
    //点赞动画
    animationMiddleHeaderItem: null,
    fabulous: {
      hasfabulous: true,
      count: 17322
    },
    tipZan: true,
    //推荐人id
    recommend_id: '',
    user_info: [],
    imgUrls: [
      'https://tuibao.renrentuibao.com/addons/zjhj_mall/core/web/temp/2d869d2f18b901d24d6cf18dda6ab067.jpg?v=1597114828',
      'https://tuibao.renrentuibao.com/addons/zjhj_mall/core/web/temp/2d869d2f18b901d24d6cf18dda6ab067.jpg?v=1597114828',
      'https://tuibao.renrentuibao.com/addons/zjhj_mall/core/web/temp/2d869d2f18b901d24d6cf18dda6ab067.jpg?v=1597114828'
    ],
    poster_status: {},
    attr: [],
    swiperIndex: 0,
    scene_value: ''
  },
  tuiHandle() {

  },
  // 轮播特效果一
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  order_text_slide: function (e) {
    var a = this
    getApp().request({
      url: getApp().api.default.order_text_slide,
      success: function (t) {
        if (t.code == 0) {
          console.log('msgListt', t)
          a.setData({
            msgList: t.data
          })
        }
      },
    });
  },
  onLoad: function (t) {

    var n = decodeURIComponent(t.scene);
    console.log('rrrrr', n)
    this.setData({
      recommend_id: t.recommend_id ? t.recommend_id : ''
    })
    if (t.xr) {
      this.user_status()
    }

    // setTimeout( ()=> {
    //   this.setData({ tipZan: false })
    // }, 5000)
    getApp().page.onLoad(this, t);
    var o = this;
    share_count = 0, is_more_comment = !(is_loading_comment = !(p = 1));
    var e = t.quick;
    if (e) {
      var a = getApp().core.getStorageSync(getApp().const.ITEM);
      if (a) var i = a.total, s = a.carGoods; else i = {
        total_price: 0,
        total_num: 0
      }, s = [];
      o.setData({
        quick: e,
        quick_list: a.quick_list,
        total: i,
        carGoods: s,
        quick_hot_goods_lists: a.quick_hot_goods_lists
      });
    }
    if ("undefined" == typeof my) {
      console.log('jjjj')
      var n = decodeURIComponent(t.scene);
      if (void 0 !== n) {
        var d = getApp().helper.scene_decode(n);
        d.uid && d.gid && (t.id = d.gid);
      }
    } else if (null !== getApp().query) {
      var c = getApp().query;
      getApp().query = null, t.id = c.gid;
    }
    o.setData({
      id: t.id
    }), o.getGoods(), o.getCommentList();


    if (wx.getStorageSync('ACCESS_TOKEN')) {
      console.log('已登录')
      getApp().page.bindParent1(wx.getStorageSync('PARENT_ID'))
    } else {
      console.log('未登录')
      var recommender_id = wx.getStorageSync('recommender_id')
      console.log('recommender_id==', recommender_id)
      var sdsd = decodeURIComponent(t.scene);
      if (t.recommend_id) {
        if (recommender_id === '' || recommender_id === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', t.recommend_id)
        } else {
          console.log('you')
        }
      }
      if (sdsd) {
        var recommender = wx.getStorageSync('recommender_id')
        var sss = getApp().helper.scene_decode(sdsd);
        console.log('dsdsds', sss, 'recommender_id', recommender)
        if (recommender === '' || recommender === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', sss.uid)
        } else {
          console.log('you')
        }
      }
    }
    this.order_text_slide()
  },
  onReady: function () {
    getApp().page.onReady(this);
  },
  onShow: function () {
    getApp().page.onShow(this), shoppingCart.init(this), specificationsModel.init(this, shoppingCart),
      gSpecificationsModel.init(this), goodsBanner.init(this), goodsInfo.init(this), goodsBuy.init(this),
      goodsRecommend.init(this), this.loadData();
    var t = getApp().core.getStorageSync(getApp().const.ITEM);
    var user_info = wx.getStorageSync('USER_INFO')
    if (t) var o = t.total, e = t.carGoods, a = this.data.goods_num; else o = {
      total_price: 0,
      total_num: 0
    }, e = [], a = 0;
    var scene_value = getApp().core.getStorageSync('scene_value');
    this.setData({
      total: o,
      carGoods: e,
      goods_num: a,
      user_info: user_info,
      scene_value: scene_value
    });


    // var obj = wx.getLaunchOptionsSync()
    // var  scene_value = obj.scene;
    // console.log('obj==',obj)
    // wx.setStorageSync('scene_value',scene_value)
  },
  user_status: function (e) {
    var o = this
    getApp().request({
      url: getApp().api.default.user_status,
      success: function (e) {
        if (e.data.is_new_user) {
          var tc = true
        } else {
          var tc = false
        }
        o.setData({
          is_new_user: e.data.is_new_user,
          kc: 1,
          tc: tc
        }),
          console.log('111111', o.data.is_new_user)

        // 0 == e.code && 
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  gos: function (e) {
    var o = this
    wx.navigateTo({
      url: '/pages/Nineactivities/Nineactivities',
    })
  },
  guanb: function (e) {
    var o = this
    o.setData({
      tc: true
    })
  },
  onHide: function () {
    getApp().page.onHide(this), shoppingCart.saveItemData(this);
  },
  onUnload: function () {
    getApp().page.onUnload(this), shoppingCart.saveItemData(this);
  },
  onPullDownRefresh: function () {
    getApp().page.onPullDownRefresh(this);
  },
  loadData: function () {
    var a = this;
    getApp().core.showLoading({
      title: "正在加载",
      mask: !0
    }), getApp().request({
      url: getApp().api.default.get_poster_status,
      success: function (e) {
        console.log('tui=====', e)
        if (0 == e.code) {
          a.setData({
            poster_status: e.data
          })
        }
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  onReachBottom: function () {
    getApp().page.onReachBottom(this);
    var t = this;
    "active" == t.data.tab_detail && t.data.drop ? (t.data.drop = !1, t.goods_recommend({
      goods_id: t.data.goods.id,
      loadmore: !0
    })) : "active" == t.data.tab_comment && t.getCommentList(!0);
  },

  //转发
  onShareAppMessage: function () {
    getApp().page.onShareAppMessage(this);
    var o = this, t = getApp().getUser();
    return {
      path: "/pages/goods/goods?id=" + this.data.id + "&recommend_id=" + t.id,
      success: function (t) {
        1 == ++share_count && o.shareSendCoupon(o);
      },
      title: o.data.goods.name,
      imageUrl: o.data.goods.pic_list[0]
    };
  },
  closeCouponBox: function (t) {
    this.setData({
      get_coupon_list: ""
    });
  },
  to_dial: function (t) {
    var o = this.data.store.contact_tel;
    getApp().core.makePhoneCall({
      phoneNumber: o
    });
  },
  getGoods: function () {
    var n = this;
    if (n.data.quick) {
      var t = n.data.carGoods;
      if (t) {
        for (var o = t.length, e = 0, a = 0; a < o; a++) t[a].goods_id == n.data.id && (e += parseInt(t[a].num));
        n.setData({
          goods_num: e
        });
      }
    }
    getApp().request({
      url: getApp().api.default.goods,
      data: {
        id: n.data.id
      },
      success: function (t) {
        if (0 == t.code) {
          var o = t.data.detail;
          WxParse.wxParse("detail", "html", o, n);
          var e = t.data;
          e.attr_pic = t.data.attr_pic, e.cover_pic = t.data.pic_list[0].pic_url;
          var a = e.pic_list, i = [];
          for (var s in a) i.push(a[s].pic_url);
          var attr = JSON.parse(e.attr)
          if (t.data.attr_group_list.length > 1 || t.data.attr_group_list[0].attr_list.length > 1) {
            e.pic_list = i, n.setData({
              goods: e,
              attr_group_list: t.data.attr_group_list,
              attr: attr,
              btn: !0
            })
          } else {
            var attr_group_list = t.data.attr_group_list
            attr_group_list[0].attr_list[0].checked = true
            console.log('attr==', attr, t.data.attr_group_list)
            e.pic_list = i, n.setData({
              goods: e,
              attr_group_list: attr_group_list,
              attr: attr,
              btn: !0
            })
          }
          // var attr_group_list = t.data.attr_group_list
          // attr_group_list[0].attr_list[0].checked = true
          // console.log('attr==',attr,t.data.attr_group_list)
          n.goods_recommend({
            goods_id: t.data.id,
            reload: !0
          }), n.selectDefaultAttr();
        }
        1 == t.code && getApp().core.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function (t) {
            t.confirm && getApp().core.switchTab({
              url: "/pages/index/index"
            });
          }
        });
        //授权刷新页面后点赞
        if (wx.getStorageSync('is_like')) {
          wx.removeStorage({
            key: 'is_like',
            success: function (res) { },
          })
          n.point_like()
        }
      }
    });
  },
  getCommentList: function (o) {
    var e = this;
    o && "active" != e.data.tab_comment || is_loading_comment || is_more_comment && (is_loading_comment = !0,
      getApp().request({
        url: getApp().api.default.comment_list,
        data: {
          goods_id: e.data.id,
          page: p
        },
        success: function (t) {
          0 == t.code && (is_loading_comment = !1, p++, e.setData({
            comment_count: t.data.comment_count,
            comment_list: o ? e.data.comment_list.concat(t.data.list) : t.data.list
          }), 0 == t.data.list.length && (is_more_comment = !1));
        }
      }));
  },
  tabSwitch: function (t) {
    "detail" == t.currentTarget.dataset.tab ? this.setData({
      tab_detail: "active",
      tab_comment: ""
    }) : this.setData({
      tab_detail: "",
      tab_comment: "active"
    });
  },
  commentPicView: function (t) {
    var o = t.currentTarget.dataset.index, e = t.currentTarget.dataset.picIndex;
    getApp().core.previewImage({
      current: this.data.comment_list[o].pic_list[e],
      urls: this.data.comment_list[o].pic_list
    });
  },
  /**点赞 */
  dianzan: function (e) {
    var that = this;
    let fabulous = that.data.fabulous
    fabulous.hasfabulous = true;
    that.setData({
      left: e.changedTouches[0].clientX,
      top: e.changedTouches[0].clientY,
      parse: true,
      fabulous: fabulous,
    })
    //接下来声明动画添加1秒，又快到慢
    var animationMiddleHeaderItem = that.data.animationMiddleHeaderItem;

    animationMiddleHeaderItem = wx.createAnimation({
      duration: 1000, // 以毫秒为单位  
      timingFunction: 'ease-out'
    });
    animationMiddleHeaderItem.opacity(0.6).scale(4, 4).step();
    //opacity动画加个透明度，scale表示放大缩小多少step表示执行完整个动画才执行下一个
    that.setData({
      animationMiddleHeaderItem: animationMiddleHeaderItem.export() //输出动画

    })
    setTimeout(() => {
      that.setData({
        parse: false,
        animationMiddleHeaderItem: null
      })
    }, 1000);
    (wx.getStorageSync('USER_INFO') && wx.getStorageSync('USER_INFO').access_token) || getApp().page.setUserInfoShow();

    this.point_like()
  },
  point_like() {
    var that = this;
    getApp().request({
      url: getApp().api.default.goods_like,
      method: 'post',
      data: {
        id: this.data.goods.id, // 商品编号
        recommend_id: this.data.recommend_id // 推荐人id，可允许为空
      },
      success: function (t) {
        if (t.code.code == 0) {
          that.data.goods.is_like == 1
          that.setData({
            goods: that.data.goods
          })
        }
        wx.showToast({
          title: t.msg,
          icon: 'none',
          duration: 2500
        })
      },
      complete: function (t) {
        if (t.data.code == -1 && t.data.msg == '请先登陆') {
          wx.setStorageSync('is_like', true)
        }
      }
    });
  }
});